﻿//-----------------------------------------------------------
// 表单自定义的数据
// Version :V 0.1.0
// ----------------------------------------------------------
_DataTable = {
    verion: "v 0.1.0",
    tableId: "",
    author: "江怀玉",
    Editable: false,
    FExpression: "5=2",
    Entity: "",
    ///列名集合
    Column: [],
    ///行值集合
    Rows: [],
    ///定义当前行
    CureentRow: "",
    SelectedIndex: -1,
    ///获取行
    getRow: function (i) {
        // this.SelectedIndex = i;
        return this.Rows[i];
    },
    FirstHTML: "",
    _DataTableURL: "AjaxData/DataTableJsonlist.aspx"
    ,
    _BusinessID: ""
    ,
    ///获取当前实体
    getEntity: function () {
        return $($("#Details table")[0]).attr("name");
    },
    ///获取列名集合
    getColumn: function () {
        var _Column = [];
        $($("#Details table tbody")[0]).find("input,select").each(function () {
            _Column.push($(this).attr("name"));
            $(this).val("");
        });
        this.Column = _Column;
    },
    ///获取当前行指定的列值
    getColumnValue: function (o) {
        if (typeof (o) == "String") {
            return eval("this.CureentRow." + o);
        }
        else {
            return eval("this.CureentRow." + Column[o]);
        }
    },
    setDataTableURL: function (xurl) {
        this._DataTableURL = xurl;
    },
    setBusinessID: function (xid) {
        if (typeof (xid) == "undefined") { this._BusinessID = ""; }
        else { this._BusinessID = xid; }

    },
    ///数据
    _DataBind: function () {
        //alert(this.Entity);
        this.Entity = this.getEntity();
        this.getColumn();
        ///this.DataBind(this.Entity);
        this.getDataSource();
    },
    ///获取数据源
    getDataSource: function () {
        //debugger
        //alert(this.FExpression);
        //debugger
        var strUrl = "";
        if (this.FExpression == "" || this.FExpression.split('=')[1] == "''") {
            strUrl = this._DataTableURL + "?r=" + Math.random() + "&order=RecID&pageindex=1&pagesize=5000&entity=" + this.Entity;
        } else {
            strUrl = this._DataTableURL + "?r=" + Math.random() + "&order=RecID&condition=" + this.FExpression + "&pageindex=1&pagesize=5000&entity=" + this.Entity;
        }
        this.getColumn();
        var col = this.Column.length;

        if (cust_DataBind) {
            cust_DataBind($("#" + this.Entity + " tr"));
        }
        this.FirstHTML = $("#" + this.Entity + " tr")[1].outerHTML;
        $.get(strUrl, function (data, textStatus) {
            //alert(data);
            if (data != "{\"total\":0,\"rows\":}" && data != "[{\"N\":\"null\"}]") {
                _DataTable.Rows = eval("(" + data + ")").rows;
            } else {
                // alert(_DataTable.Entity
                //var o = clone(eval(_DataTable.Entity));
                //_DataTable.Rows.push(o);
            }
            _DataTable.Render();
            //debugger;
            var trs = $("#" + _DataTable.Entity + " tr");
            for (var i = 0; i < _DataTable.Rows.length; i++) {
                $(trs[i + 1]).JSONToForm(_DataTable.Rows[i]);
                //增加行时，加载自定义事件
                if (typeof (fnCustomeGetDataSourceEvent) == "function") {
                    fnCustomeGetDataSourceEvent(trs, i);
                }
            }

            var th = $("<thead><thead>").append($("#" + _DataTable.Entity + " tr:first"));
            $("#" + _DataTable.Entity).prepend(
                th
            );
            $("#" + _DataTable.Entity + " tr:first").remove();
            if (typeof (parent._CancelFlag) != "undefined") {
                //退回标标/ID/草稿
                if (parent._CancelFlag == "4" || _DataTable._BusinessID == "" || getPar1("statues") == "2") {
                    {
                        if (_DataTable.Entity != 'bpm_OpenClassBudgetDetails') {
                            $("#" + _DataTable.Entity + " thead:first").prepend(
                            $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_DataTable.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_DataTable.DeleteRow();return false;'>删除</button></td></tr>")
                             );
                        }
                    }
                } else {
                    //明细表权限。
                    if (typeof (DetailsFormRoles) != "undefined") {
                        if (DetailsFormRoles != null) {
                            if (typeof (DetailsFormRoles.EditorPropertyValue) != "undefined") {
                                if (DetailsFormRoles.EditorPropertyValue.length > 0) {
                                    if (_DataTable.Entity != 'bpm_OpenClassBudgetDetails') {
                                        $("#" + _DataTable.Entity + " thead:first").prepend(
                                    $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_DataTable.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_DataTable.DeleteRow();return false;'>删除</button></td></tr>")
                                     );
                                    }
                                }
                            }

                        }
                    }

                }
            }
            else {
                //退回标标/ID/草稿
                if (_DataTable._BusinessID == "") {
                    if (_DataTable.Entity != 'bpm_OpenClassBudgetDetails') {
                        $("#" + _DataTable.Entity + " thead:first").prepend(
                    $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_DataTable.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_DataTable.DeleteRow();return false;'>删除</button></td></tr>")
                );
                    }
                } else {
                    //非流程
                    //alert(getPar1("entity").indexOf("bpm_"));
                    if (getPar1("entity").indexOf("bpm_") == -1 || getPar1("statues") == "2") {
                        $("#" + _DataTable.Entity + " thead:first").prepend(
                    $("<tr><td colspan=\"" + col + "\"><button id='_btnNew' onclick='_DataTable.AddRow();return false;'>新增</button><button id='_btnNew' onclick='_DataTable.DeleteRow();return false;'>删除</button></td></tr>")
                    );

                    }
                }
            }

            if (data != "{\"total\":0,\"rows\":}") {
                fnSelected();
            }
            //$("#" + _DataTable.Entity + " tr:first").remove();
        });
    },
    ///重写控制
    Render: function () {
        //var trs = $($($("#" + this.Entity + " tr")[1]).html());
        $($("#" + this.Entity + " tr")[1]).remove();

        if (this.Rows.length > 0) {
            for (var i = 0; i < this.Rows.length; i++) {
                var trs = $(this.FirstHTML.replace("data_row", "id" + i));
                trs.appendTo($($("#Details table")));
                //debugger
                if (typeof (parent._CancelFlag) != "undefined") {
                    //退回标标/ID/草稿
                    if (parent._CancelFlag != "4" && _DataTable._BusinessID != "" && getPar1("statues") != "2") {

                        if (typeof (DetailsFormRoles) != "undefined") {
                            if (DetailsFormRoles != null) {
                                if (typeof (DetailsFormRoles.EditorPropertyValue) != "undefined") {
                                    if (DetailsFormRoles.EditorPropertyValue.length > 0) {
                                        trs.setInputReadOnly(false);
                                    } else { trs.setInputReadOnly(true); }
                                } else { trs.setInputReadOnly(true); }
                            } else { trs.setInputReadOnly(true); }
                        } else { trs.setInputReadOnly(true); }
                    }
                }
                else {
                    if (_DataTable._BusinessID != "") {
                        if (getPar1("entity").indexOf("bpm_") > -1) {
                            trs.setInputReadOnly(true);
                        }
                    }
                }
            }
            //this.AddRow();
        }
        //初始化
        if (typeof (initControls) == "function") {
            var _time = $("input[type=Time]");
            initControls(_time, "Time");
            var _date = $("input[type=Date]");
            initControls(_date, "Date");
        }
    },
    //增加行
    AddRow: function () {
        var o;
        if (arguments.length > 0) {
            o = arguments[0];
        } else {
            o = clone(eval(_DataTable.Entity));
        }
        this.Rows.push(o);
        $("#Details tbody tr").unbind();
        var i = this.Rows.length - 1
        var trs = $(this.FirstHTML.replace("data_row", "id" + this.Rows.length - 1));
        trs.appendTo($($("#Details table")));
        trs.find("input").bind('blur', function () {
            _DataTable.Modify();
        });
        $(trs).JSONToForm(_DataTable.Rows[i]);
        fnSelected();
        if (typeof (fnCustomeRowAddEvent) == "function") {
            fnCustomeRowAddEvent(trs,i);
        }
        //初始化
        if (typeof (initControls) == "function") {
            var _time = $("input[type=Time]");
            initControls(_time, "Time");
            var _date = $("input[type=Date]");
            initControls(_date, "Date");

        }
    },
    //增加行
    AddRowByEntity: function (obj) {
        //debugger;
        if (typeof (obj) != "undefined") {
            this.Rows.push(obj);
            $("#Details tbody tr").unbind();
            // $("#" + _DataTable.Entity + " tr:first").remove();
            var i = this.Rows.length - 1
            var trs = $(this.FirstHTML.replace("data_row", "id" + this.Rows.length - 1));
            trs.appendTo($($("#Details table")));
            trs.find("input").bind('blur', function () {
                _DataTable.Modify();
            });
            $(trs).JSONToForm(_DataTable.Rows[i]);
            fnSelected();
            $("#" + _DataTable.Entity + " tr:last").remove();
            //初始化
            if (typeof (initControls) == "function") {
                var _time = $("input[type=Time]");
                initControls(_time, "Time");
                var _date = $("input[type=Date]");
                initControls(_date, "Date");

            }

        }
    },
    //删除行
    DeleteRow: function () {
        //debugger
        var i = 0;
        //$("#Details tbody tr")
        $("#Details tbody .selected").remove();
        this.Rows.REMOVE(this.CureentRow);
    },
    //修改
    Modify: function () {
        $("#Details tbody tr.selected").SetFormValueToObject(this.CureentRow);
    },
    //选定某条数据
    Selected: function (e) {
        this.SelectedIndex = 0;
        var trs1 = $("#Details tbody tr");
        trs1.each(function (em) {
            var _class = $(trs1[em]).attr("class");
            if (_class == undefined) {
                return;
            }
            if ($(trs1[em]).attr("class").indexOf("selected") > -1) {
                _DataTable.SelectedIndex = em;
            }
        });
        if (_DataTable.SelectedIndex.toString() == "NaN") {
            _DataTable.SelectedIndex = 0;
        }
        this.CureentRow = this.Rows[this.SelectedIndex];
        //$("#test")[0].innerHTML = (this.CureentRow.ID);
    },
    GetCurrentRow: function () {
        debugger
        var trs = $("#Details tbody tr");
        trs.each(function (em) {
            if ($(trs[em]).attr("class").indexOf("selected") >= 0) {
                return $(trs[em]);
            }

        }
        )
        return null;
    },
    ///在数据提交前将表格中数据跟数据对象同步
    GetData: function () {
        var trs = $("#Details tbody tr");
        trs.each(function (em) {
            $("#Details tbody tr").removeClass("selected");
            $(trs[em]).addClass("selected");
            _DataTable.Selected($(trs[em]));
            _DataTable.Modify();
        });


    }
}
//选择
var fnSelected = function () {
    $("#Details tbody tr").bind('click', function (evt) {
        _DataTable.Modify();
        $("#Details tr").removeClass("selected");
        $(this).addClass("selected");
        _DataTable.Selected($(this));
    });
}

/// 检查数据
var fnCheckEmpty = function () {
    var trs = $("#Details tbody tr");
    if (trs.length <= 1) {
        return true;
    }
    trs.each(function (em) {
        var t = $(trs[em]).find("input");
        var _t = "";
        t.each(function (_em) {
            _t = _t + $(t[_em]).val();
        });
        if (_t == "") {
            return false;
        }
    });
    return true;
}

function cust_DataBind(o) {
    var selective = o.find("select");
    selective.each(function (s) {
        //debugger
        try {
            var o = eval("ds_Detail_" + selective[s].name);
            if (typeof (o) != "undefined") {
                $(selective).comBind(o, "ValueMember", "DisplayMember");
            }
        } catch (err) {
        }
    });
}

